package Test;

import vo.Student;

import java.util.*;
import java.util.stream.Collectors;

public class Test4 {

    static List<Student> list = Arrays.asList(new Student(1, "ff", "中国"),
            new Student(2, "bb", "美国"),
            new Student(3, "cc", "法国"),
            new Student(4, "dd", "日本"),
            new Student(5, "dd", "日本"));

    //特定集合类接受
    public static void test1() {
        TreeSet<Integer> collect = list.stream().map(e -> e.getId()).collect(Collectors.toCollection(TreeSet::new));
        System.out.println(collect);
    }

    //maxBy
    public static void test2() {
        Optional<Student> collect = list.stream().collect(Collectors.minBy(Comparator.comparing(e -> e.getId())));
        System.out.println(collect.orElse(new Student()).getId());
    }

    //partitionBy
    public static void test3(){
        Map<Boolean, List<Student>> collect = list.stream().collect(Collectors.partitioningBy(e -> {
            return e.getId() > 3;
        }));

        collect.forEach((k,v) ->{
            if (k){
                v.forEach(System.out::println);
            }
        });
    }

    //joining
    public static void test4(){
        String collect = list.stream().map(Student::getName).collect(Collectors.joining(",", "[", "]"));
        System.out.println(collect);
    }

    //StringJon
    public static void test5(){
        StringJoiner st=new StringJoiner(",");
        for (int i = 0; i <10 ; i++) {
            st.add(String.valueOf(i));
        }
        System.out.println(st);
    }
    
    //string.join
    public static void test6(){
        String collect = list.stream().map(Student::getName).collect(Collectors.joining(","));
        List<String> list = Arrays.asList(collect.split(","));
        String join = String.join(",", list);
        System.out.println(join);
    }

    //分组计算数量
    public static void test7(){
        Map<String, Long> collect = list.stream().collect(Collectors.groupingBy(Student::getName, Collectors.counting()));
        System.out.println(collect);
    }

    //mapping
    public static void test8(){
        Map<String, List<Integer>> collect = list.stream().collect(Collectors.groupingBy(Student::getName, Collectors.mapping(Student::getId, Collectors.toList())));
        System.out.println(collect);
    }


        public static void main(String[] args) {
        test8();
    }
}
